/*
 * BibliotecaAddBooksFrame.java
 *
 * Created on 29 settembre 2008, 14.01
 */
package it.biblioteca;

import it.biblioteca.util.Book;
import it.biblioteca.util.BookList;
import it.biblioteca.util.ComboFields;
import it.biblioteca.util.UtilFileOperation;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;

/**
 *
 * @author  giovine
 */
public class BibliotecaAddBooksFrame extends javax.swing.JFrame {

    BookList my_list;
    ComboFields my_fields;
    ImageIcon im;

    /** Creates new form BibliotecaAddBooksFrame */
    public BibliotecaAddBooksFrame(BookList b, ComboFields c) {
        this.my_list = b;
        this.my_fields = c;

       
        
        //Set icon of frame
        im = new ImageIcon("files/icon_desktop.png");
       
        
        
        //this.location_combo.gets
        initComponents();
        
        this.setIconImage(im.getImage());
    }

    
    public void addComboItem(String s){
        this.location_combo.addItem(s);
        
    }
    
    public void removeComboItem(){
        this.location_combo.removeAllItems();
        ArrayList <String> list = this.my_fields.getFields();
        
        Iterator i = list.iterator();
        
        while(i.hasNext()){
            this.location_combo.addItem((String)i.next());
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        title_tf = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        author_tf = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        home_tf = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        ok_bt = new javax.swing.JButton();
        clean_bt = new javax.swing.JButton();
        close_bt = new javax.swing.JButton();
        location_combo = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        trans_tf = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        note_area = new javax.swing.JTextArea();
        jLabel8 = new javax.swing.JLabel();
        copy_check = new javax.swing.JCheckBox();
        jMenuBar1 = new javax.swing.JMenuBar();
        file_menu = new javax.swing.JMenu();
        save_item = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        close_item = new javax.swing.JMenuItem();
        edit_menu = new javax.swing.JMenu();
        edit_location_item = new javax.swing.JMenuItem();
        about_menu = new javax.swing.JMenu();
        info_item = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("BiblioTeCa - Aggiungi libri.");
        setResizable(false);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/images/Header.png"))); // NOI18N

        jLabel2.setForeground(new java.awt.Color(255, 0, 0));
        jLabel2.setText("Aggiungi nuovi libri");

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        jLabel3.setText("Titolo del libro:");

        jLabel4.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        jLabel4.setText("Autore del libro:");

        jLabel5.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        jLabel5.setText("Casa editrice del libro:");

        jLabel6.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        jLabel6.setText("Locazione del libro:");

        ok_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_ok_add.png"))); // NOI18N
        ok_bt.setText("Aggiungi libro");
        ok_bt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ok_btActionPerformed(evt);
            }
        });

        clean_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_clean.png"))); // NOI18N
        clean_bt.setText("Pulisci i campi");
        clean_bt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clean_btActionPerformed(evt);
            }
        });

        close_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_close.png"))); // NOI18N
        close_bt.setText("Chiudi");
        close_bt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                close_btActionPerformed(evt);
            }
        });

        ArrayList <String> a = this.my_fields.getFields();

        Iterator i = a.iterator();

        while(i.hasNext()){
            location_combo.addItem((String)i.next());
        }

        jLabel7.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        jLabel7.setText("Traduttore del libro:");

        note_area.setColumns(20);
        note_area.setLineWrap(true);
        note_area.setRows(5);
        jScrollPane1.setViewportView(note_area);

        jLabel8.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        jLabel8.setText("Note:");

        copy_check.setFont(new java.awt.Font("DejaVu Sans", 1, 12));
        copy_check.setText("Fotocopiato");

        file_menu.setText("File");

        save_item.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        save_item.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_save.png"))); // NOI18N
        save_item.setText("Salva Database");
        save_item.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                save_itemActionPerformed(evt);
            }
        });
        file_menu.add(save_item);
        file_menu.add(jSeparator1);

        close_item.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        close_item.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_close.png"))); // NOI18N
        close_item.setText("Chiudi");
        close_item.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                close_itemActionPerformed(evt);
            }
        });
        file_menu.add(close_item);

        jMenuBar1.add(file_menu);

        edit_menu.setText("Modifica");

        edit_location_item.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        edit_location_item.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_edit.png"))); // NOI18N
        edit_location_item.setText("Modifica locazioni");
        edit_location_item.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                edit_location_itemActionPerformed(evt);
            }
        });
        edit_menu.add(edit_location_item);

        jMenuBar1.add(edit_menu);

        about_menu.setText("Aiuto");

        info_item.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        info_item.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_info.png"))); // NOI18N
        info_item.setText("Informazioni");
        info_item.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                info_itemActionPerformed(evt);
            }
        });
        about_menu.add(info_item);

        jMenuBar1.add(about_menu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(92, 92, 92)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addComponent(jLabel2)))
                .addContainerGap(128, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 92, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(copy_check)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(location_combo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(title_tf)
                        .addComponent(home_tf)
                        .addComponent(author_tf, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                        .addComponent(trans_tf))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32))
            .addGroup(layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addComponent(ok_bt)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(clean_bt)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(close_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(74, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(14, 14, 14)
                        .addComponent(jLabel4)
                        .addGap(14, 14, 14)
                        .addComponent(jLabel5)
                        .addGap(14, 14, 14)
                        .addComponent(jLabel6))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(title_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(author_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(home_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(location_combo, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addComponent(trans_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(14, 14, 14)
                        .addComponent(copy_check))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(clean_bt)
                    .addComponent(ok_bt)
                    .addComponent(close_bt))
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void close_btActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_close_btActionPerformed
    BibliotecaInitialChoice init = new BibliotecaInitialChoice(this.my_list, this.my_fields);
    init.setLocationRelativeTo(null);
    init.setVisible(true);

    dispose();
}//GEN-LAST:event_close_btActionPerformed

private void clean_btActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clean_btActionPerformed
    this.title_tf.setText("");
    this.author_tf.setText("");
    this.home_tf.setText("");
    this.trans_tf.setText("");
    this.note_area.setText("");
    this.location_combo.setSelectedIndex(0);
    this.copy_check.setSelected(false);
}//GEN-LAST:event_clean_btActionPerformed

private void ok_btActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ok_btActionPerformed
    //Check fields
    String title = this.title_tf.getText();
    Book book = new Book();
    if (title.equals("")) {
        BibliotecaAddWarning war = new BibliotecaAddWarning("Il campo \"Titolo\" deve essere riempito.");
        war.setLocationRelativeTo(null);
        war.setVisible(true);
    } else {
            String author = this.author_tf.getText();
            if (author.equals("")) {
                BibliotecaAddWarning war = new BibliotecaAddWarning("Il campo \"Autore\" deve essere riempito.");
                war.setLocationRelativeTo(null);
                war.setVisible(true);
            }  else {
                    String home = this.home_tf.getText();
                    if (home.equals("")) {
                        BibliotecaAddWarning war = new BibliotecaAddWarning("Il campo \"Casa editrice\" deve essere riempito.");
                        war.setLocationRelativeTo(null);
                        war.setVisible(true);
                    } else {
                            String translater = this.trans_tf.getText();
                            
                            String location = (String) this.location_combo.getSelectedItem();

                            book.setTitle(title);
                            book.setAuthor(author);
                            book.setHouse(home);
                            book.setLocation(location);
                            book.setTranslater(translater);
                            book.setNote(this.note_area.getText());
                            
                            if(this.copy_check.isSelected()){
                                System.out.println("Questo libro è fotocopiato.");
                                book.setCopy(true);
                            }
                            else{
                                System.out.println("Questo libro non è fotocopiato.");
                                book.setCopy(false);
                            }

                            this.my_list.addBook(book);

                            this.title_tf.setText("");
                            this.author_tf.setText("");
                            this.home_tf.setText("");
                            this.trans_tf.setText("");
                            this.note_area.setText("");
                            this.copy_check.setSelected(false);
                        }
                    }
                }
            
        
    
}//GEN-LAST:event_ok_btActionPerformed

private void close_itemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_close_itemActionPerformed
    UtilFileOperation.saveDataBase(my_list);
    UtilFileOperation.saveFields(my_fields);
    
    BibliotecaInitialChoice init = new BibliotecaInitialChoice(this.my_list, this.my_fields);
    init.setLocationRelativeTo(null);
    init.setVisible(true);

    dispose();
}//GEN-LAST:event_close_itemActionPerformed

private void save_itemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_save_itemActionPerformed
    UtilFileOperation.saveDataBase(this.my_list);
}//GEN-LAST:event_save_itemActionPerformed

private void edit_location_itemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_edit_location_itemActionPerformed
    BibliotecaEditLocationFrame edit_location = new BibliotecaEditLocationFrame(this.my_fields,this,true);
    edit_location.setLocationRelativeTo(null);
    edit_location.setVisible(true);
}//GEN-LAST:event_edit_location_itemActionPerformed

private void info_itemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_info_itemActionPerformed
    BibliotecaInfoFrame info = new BibliotecaInfoFrame();
    info.setLocationRelativeTo(null);
    info.setVisible(true);
}//GEN-LAST:event_info_itemActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu about_menu;
    private javax.swing.JTextField author_tf;
    private javax.swing.JButton clean_bt;
    private javax.swing.JButton close_bt;
    private javax.swing.JMenuItem close_item;
    private javax.swing.JCheckBox copy_check;
    private javax.swing.JMenuItem edit_location_item;
    private javax.swing.JMenu edit_menu;
    private javax.swing.JMenu file_menu;
    private javax.swing.JTextField home_tf;
    private javax.swing.JMenuItem info_item;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JComboBox location_combo;
    private javax.swing.JTextArea note_area;
    private javax.swing.JButton ok_bt;
    private javax.swing.JMenuItem save_item;
    private javax.swing.JTextField title_tf;
    private javax.swing.JTextField trans_tf;
    // End of variables declaration//GEN-END:variables
}
